package com.example.service.impl;

import com.example.dao.CarTypeDao;
import com.example.entity.result.Result;
import com.example.entity.vo.CarType;
import com.example.entity.vo.UserType;
import com.example.service.CarTypeService;
import com.example.utils.DataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class CarTypeServiceImpl implements CarTypeService {

    @Autowired
    private CarTypeDao carTypeDao;

    /**
     * 查询
     *
     * @param key   查询方式
     * @param value 查询参数
     */
    @Override
    public Result query(String key, String value) {
        DataUtils.paramCheckEmpty(key, value);
        switch (key) {
            case "all":
                return DataUtils.getResult(Result.SUCCESS, "carTypeAll",
                        carTypeDao.selectAll());
            case "byId":
                return DataUtils.getResult(Result.SUCCESS, "carType",
                        carTypeDao.selectById(DataUtils.paramToInteger(value)));
            case "byName":
                return DataUtils.getResult(Result.SUCCESS, "carType",
                        carTypeDao.selectByName(value));
            case "byLike":
                return DataUtils.getResult(Result.SUCCESS, "carType",
                        carTypeDao.selectByLike(value));
            default:
                return DataUtils.getResult(Result.PARAMETER_INVALID);
        }
    }

    @Override
    public Result insertCarType(CarType carType) {
        if (StringUtils.isEmpty(carType.getCtName())) {
            return DataUtils.getResult(Result.PARAMETER_EMPTY);
        }
        if (carTypeDao.selectByName(carType.getCtName()) != null) {
            return DataUtils.getResult(Result.ERROR, "该类型已存在");
        }
        int add = carTypeDao.add(carType);
        return (add > 0) ? DataUtils.getResult(Result.SUCCESS) : DataUtils.getResult(Result.ERROR);
    }

    @Override
    public Result updateCarType(CarType carType) {
        if (StringUtils.isEmpty(carType.getCtName())) {
            return DataUtils.getResult(Result.PARAMETER_EMPTY);
        }
        if (carTypeDao.selectByName(carType.getCtName()) != null) {
            return DataUtils.getResult(Result.ERROR, "该类型已存在");
        }
        if (DataUtils.dataCompare(carType, carTypeDao.selectById(carType.getCtId()), UserType.class) == 1) {
            return DataUtils.getResult(Result.SUCCESS, "无更新");
        }
        int update = carTypeDao.update(carType);
        return (update > 0) ? DataUtils.getResult(Result.SUCCESS) : DataUtils.getResult(Result.ERROR);
    }

    @Override
    public Result deleteCarType(int ctId) {
        if (carTypeDao.selectById(ctId) == null) {
            return DataUtils.getResult(Result.ERROR, "找不到该类型");
        }
        int delete = carTypeDao.delete(ctId);
        return (delete > 0) ? DataUtils.getResult(Result.SUCCESS) : DataUtils.getResult(Result.ERROR);
    }

}
